﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR.InteractionSystem;

public class Ban2Throwable : Throwable
{
    //爆炸特效.
    public GameObject exploredEffect;

    //当本物体被扔出去时，触发此函数.
    protected override void OnDetachedFromHand(Hand hand)
    {
        Debug.LogWarning("我被扔出去了！！！惨啊1！！");
        base.OnDetachedFromHand(hand);

        //3秒后爆炸.
        Invoke("Explored", 3f);
    }

    //爆炸函数.
    void Explored()
    {
        //复制爆炸特效.
        Instantiate(exploredEffect, transform.position, Quaternion.identity);
        //判断周围一定范围内，是否有layer为Enemy的碰撞器，如果有，则表示有敌人，需要对其造成伤害.
        int layerMask = LayerMask.NameToLayer("Enemy");
        layerMask = 1 << layerMask;
        Collider[] colArray = Physics.OverlapSphere(transform.position, 4, layerMask);
        //表示，有敌人在爆炸范围之内.
        if(colArray!=null && colArray.Length>0)
        {
            Debug.LogWarning("表示，有敌人在爆炸范围之内");
            for(int i=0;i< colArray.Length;i++)
            {
                Ban2Enemy enemy = colArray[i].GetComponent<Ban2Enemy>();
                if(enemy!=null)
                {
                    enemy.OnHurt();
                }
            }
        }

        //删除本物体.
        //Destroy(gameObject);
    }
}
